package com.cetc.nsfx.controller.fwglyjk;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cetc.nsfx.service.IntelligentService;
import com.cetc.nsfx.util.BaseWebUtil;

import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * Created by Administrator on 2016/1/12 0012.
 */
@Controller
@RequestMapping("/intelligent")
public class IntelligentController extends BaseWebUtil{
	
	private static final int BUFFER_SIZE = 64 * 1024 ;
	
	@Autowired
	private IntelligentService intelligentService;
	
    @RequestMapping("/warn")
    public String warn(){
        return "intelligent/warn";
    }

    @RequestMapping("/main")
    public String main(HttpServletRequest req){
    	req.getSession().setAttribute("current_menu", "2000");
        return "intelligent/main";
    }
    
    @RequestMapping("/export")
    public void export(HttpServletRequest request,HttpServletResponse response){
    	Map<String,Object>map = getMapParams(request);
    	try {
			File file = new File("商机报表.xls");
			if (!file.exists()) {
				file.createNewFile();
			}
			WritableWorkbook book = Workbook.createWorkbook(file); 
			WritableSheet sheet = book.createSheet("商机列表", 0);
			intelligentService.writeExportData(sheet,map);
			book.write();
			book.close();
			
			try {
				FileInputStream fis = new FileInputStream(file);
				response.reset();
				response.setHeader("Content-Disposition","attachment;filename=" + encodeFilename(request,"商机报表.xls"));
				response.setHeader("Connection", "close");
				response.setHeader("Content-Type","application/x-download");
//				getResponse().setContentType("application/vnd.ms-excel;charset=gb2312");  
				OutputStream outputStream = response.getOutputStream();
				int bytesRead = 0;
		        byte buffer[] = new byte[BUFFER_SIZE];
		        try {
					while((bytesRead = fis.read(buffer, 0, BUFFER_SIZE)) != -1) {
						outputStream.write(buffer, 0, bytesRead);
					}
					outputStream.flush();
				} catch (IOException e) {
					throw e;
				}finally{
					fis.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally {
				if(file != null && file.exists()) {
					file.delete();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
    }
    
    
    /**
	 * 根据用户查询角色具有的菜单权限
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sourcelist",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> roleMenuData(HttpServletRequest request) {

		Map<String, Object> reValue = new HashMap<String, Object>();
		List<Map<String,Object>> pageData = new ArrayList<Map<String,Object>>();
		pageData = intelligentService.getIntelligentSourceTotal(getMapParams(request));
		reValue.put("data",pageData);
		return reValue;
	}
}
